package com.jty.module_login.ui.fragment

import android.graphics.Color
import android.os.Build
import android.text.InputType
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.lib_base.config.AppConstants
import com.jty.lib_base.base.BaseFragment
import com.jty.lib_base.widget.EditTextMonitor
import com.jty.module_login.BR
import com.jty.module_login.R
import com.jty.module_login.databinding.LoginRegisterFragmentBinding
import com.jty.module_login.viewmodel.RePasswordViewModel
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.PublishSubject


/**
 * author: zhouyh
 * created on: 2021/9/17 11:35 下午
 * description:找回页面
 */
@Route(path = AppConstants.Router.Login.F_RESET_PWD)
class ResetPwdFragment : BaseFragment<LoginRegisterFragmentBinding, RePasswordViewModel>() {


    override fun initContentView(): Int {
        return R.layout.login_register_fragment
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }


    override fun initData() {

        viewModel.tvTitle.set("找回密码")
        initBtnState()
    }

    private fun initBtnState() {
        binding.btnOk.isEnabled = false
        val accountSubject = PublishSubject.create<String>()
        val codeSubject = PublishSubject.create<String>()
        val pwdSubject = PublishSubject.create<String>()
        val rePwdSubject = PublishSubject.create<String>()
        binding.etPhoneNumber.addTextChangedListener(EditTextMonitor(accountSubject))
        binding.etCheckCode.addTextChangedListener(EditTextMonitor(codeSubject))
        binding.etNewPwd.addTextChangedListener(EditTextMonitor(pwdSubject))
        binding.etCheckPwd.addTextChangedListener(EditTextMonitor(rePwdSubject))
        viewModel.addSubscribe(
            Observable.combineLatest(accountSubject, codeSubject, pwdSubject, rePwdSubject)
            { account: String, code: String, pwd: String, rePwd: String ->
                account.isNotBlank() && code.isNotBlank() && pwd.isNotBlank() && rePwd.isNotBlank()
            }
                .observeOn(AndroidSchedulers.mainThread()).subscribe {

                    binding.btnOk.setTextColor(Color.parseColor("#ffffff"))
                    binding.btnOk.isEnabled = it
                    binding.btnOk.setBackgroundResource(if (it) R.drawable.shape_round_white else R.drawable.green_btn_corner_10dp)



                }
        )
}

@RequiresApi(Build.VERSION_CODES.M)
override fun initViewObservable() {
    viewModel.uc.passwordVisibleEvent.observe(this) {
        binding.ivPasswordEye.setImageResource(if (it) R.drawable.ic_eye else R.drawable.ic_eye_hide)
        if (it) {
            binding.etNewPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
        } else {
            binding.etNewPwd.transformationMethod = PasswordTransformationMethod.getInstance()
        }
    }

    viewModel.uc.rePasswordVisibleEvent.observe(this) {
        binding.ivRepasswordEye.setImageResource(if (it) R.drawable.ic_eye else R.drawable.ic_eye_hide)
        if (it) {
            binding.etCheckPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
        } else {
            binding.etCheckPwd.transformationMethod = PasswordTransformationMethod.getInstance()
        }
    }


    viewModel.uc.sendCodeClickEvent.observe(this) {
        if (it) {
            binding.tvGetCode.isEnabled = true
            binding.tvGetCode.setTextColor(resources.getColor(R.color.green_9c, null))
        } else {
            binding.tvGetCode.isEnabled = false
            binding.tvGetCode.setTextColor(resources.getColor(R.color.white_dd, null))

        }
    }
}
}